package com.filldream.fastboot.common.exception;

import com.filldream.fastboot.common.entity.ErrContent;
import com.filldream.fastboot.common.entity.ErrorCode;
import lombok.Data;

/**
 * @author RickSun
 */
@Data
public class GlobalException extends  RuntimeException{

    private static final long serialVersionUID = -238091758285157331L;
    private ErrContent errContent;
    private Boolean sendDd = false;

    /**
     * 抛出具体报错
     * @param errContent 错误码
     * @param errMsg    错误具体信息
     * @param sendDd    是否发送钉钉消息
     */
    public GlobalException(ErrContent errContent,String errMsg,Boolean sendDd) {
        super(errMsg);
        this.sendDd = sendDd == null ? false : sendDd;
        this.errContent = errContent;
    }


    /**
     * 抛出具体报错
     * @param errContent 错误码
     * @param errMsg    错误具体信息
     */
    public GlobalException(ErrContent errContent,String errMsg) {
        super(errMsg);
        this.errContent = errContent;
    }


    /**
     * 默认报错
     * @param errContent 错误码
     */
    public GlobalException(ErrContent errContent) {
        super("No errMsg");
        this.errContent = errContent;
    }

    /**
     * 默认报错
     * @param errContent 错误码
     * @param sendDd    是否发送钉钉消息
     */
    public GlobalException(ErrContent errContent,Boolean sendDd) {
        super("No errMsg");
        this.sendDd = sendDd == null ? false : sendDd;
        this.errContent = errContent;
    }

    /**
     * 自定义异常构造器
     * @param msg 错误信息
     */
    public GlobalException(String msg) {
        super(ErrorCode.SERVER_BUSY + ":" + msg);
        ErrContent errContent = new ErrContent();
        errContent.setErrorCode( ErrorCode.SERVER_BUSY );
        errContent.setContent(msg);
        this.errContent = errContent;
    }

    /**
     * 自定义异常构造器
     * @param msg 错误信息
     * @param sendDd    是否发送钉钉消息
     */
    public GlobalException(String msg,Boolean sendDd) {
        super(ErrorCode.SERVER_BUSY + ":" + msg);
        ErrContent errContent = new ErrContent();
        errContent.setContent(msg);
        this.sendDd = sendDd == null ? false : sendDd;
        this.errContent = errContent;
    }

    /**
     *  已知错误类型，抛出异常
     * @param errContent
     * @param e
     */
    public GlobalException(ErrContent errContent,Exception e) {
        super(e.getMessage(),e);
        this.errContent = errContent;
    }

    public GlobalException(String msg,Exception e) {
        super(e.getMessage(),e);
        this.errContent = ErrorCode.SERVER_BUSY.reMsg(msg);
    }

    /**
     * 已知错误类型，抛出异常
     * @param errContent
     * @param e
     * @param sendDd    是否发送钉钉消息
     */
    public GlobalException(ErrContent errContent,Exception e,Boolean sendDd) {
        super(e.getMessage(),e);
        this.sendDd = sendDd == null ? false : sendDd;
        this.errContent = errContent;
    }

    public ErrContent getErrContent() {
        return errContent;
    }

    @Override
    public String toString() {
        return this.errContent.getContent();
    }
}